﻿using System;
using System.Web.Mvc;
using Netkk.Common.Models;

namespace Netkk.Core.ActionResults
{
    public class VCalendarResult : ActionResult
    {
        public Appointment Appointment { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (Appointment == null)
            {
                throw new ArgumentNullException("Appointment");
            }


            context.HttpContext.Response.Clear();
            context.HttpContext.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.ics", Appointment.Title));
            context.HttpContext.Response.ContentType = "text/calendar";

            VCalendar.VCalendar vCalendar = new VCalendar.VCalendar(new VCalendar.VCalendar.VEvent
                                                    {
                                                        HoleDay = false,
                                                        Uid = Appointment.Id.ToString(),
                                                        DtStart = Appointment.Date,
                                                        DtEnd = Appointment.Date.AddHours(3),
                                                        DtStamp = DateTime.Now,
                                                        Summary = Appointment.Title,
                                                        Organizer = ".NET Stammtisch Konstanz-Kreuzlingen",
                                                        Location = Appointment.Location,
                                                        Description = Appointment.Description,
                                                        Url = String.Format("http://www.dotnetkk.de/Appointments/Details/{0}", Appointment.Id),
                                                        Classification = VCalendar.VCalendar.ClassificationType.Public,
                                                        Priority = VCalendar.VCalendar.Priority.None
                                                    });

            context.HttpContext.Response.Write(vCalendar.ToString());
        }

    }
}